<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="商品编号" prop="goodsSn">
        <el-input
          v-model="queryParams.goodsSn"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否上架" prop="isOnSale">
        <el-select v-model="queryParams.isOnSale" placeholder="请选择是否上架" clearable size="small">
          <el-option
            v-for="dict in isOnSaleOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品分类ID" prop="catId">
        <el-input
          v-model="queryParams.catId"
          placeholder="请输入商品分类ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分类名称" prop="catName">
        <el-input
          v-model="queryParams.catName"
          placeholder="请输入商品分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品系列ID" prop="seriesId">
        <el-input
          v-model="queryParams.seriesId"
          placeholder="请输入商品系列ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="goodsSupplierName">
        <el-select v-model="queryParams.goodsSupplierName" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in supplierOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['goods:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goods:goods:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="setSeries"
          v-hasPermi="['goods:goods:edit']"
        >设为同一系列</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="setPriceToShop"
          v-hasPermi="['goods:goods:edit']"
        >同步商品价格至门店</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="tbAllGoods"
        >同步商品至全部店铺</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="tbCgtpGoods"
        >同步采购平台商品</el-button>
      </el-col>
      
    </el-row>

    <el-table v-loading="loading" border stripe :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="唯一ID" align="center" prop="id" /> -->
      <el-table-column label="商品编号" align="center" prop="goodsSn" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品主图原图" align="center" prop="goodsImg" width="100">
        <template slot-scope="scope">
          <img v-if="scope.row.goodsImg" :src="scope.row.goodsImg" alt="" width="100%" height="100%">
        </template>
      </el-table-column>
      <el-table-column label="是否上架" align="center" prop="isOnSale" :formatter="isOnSaleFormat" />
      <el-table-column label="平台基准价格" align="center" prop="price" />
      <el-table-column label="商店成本价格" align="center" prop="costPrice" />
      <el-table-column label="商品排序" align="center" prop="sort" />
      <el-table-column label="商品系列ID" align="center" prop="seriesId" />
      <el-table-column label="商品分类ID" align="center" prop="catId" />
      <el-table-column label="商品分类名称" align="center" prop="catName" />
      <el-table-column label="供应商" align="center" prop="goodsSupplierName" />
      
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="openPreview(scope.row)"
          >预览</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改商品基础信息对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="是否上架">
          <el-select v-model="form.isOnSale" placeholder="请选择是否上架">
            <el-option
              v-for="dict in isOnSaleOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="平台基准价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入平台基准价格" />
        </el-form-item>
        <el-form-item label="商品主图原图" prop="goodsImg">
          <el-input v-model="form.goodsImg" placeholder="请输入商品主图原图" />
        </el-form-item>
        <el-form-item label="商品排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入商品排序" />
        </el-form-item>
        <el-form-item label="商品分类ID" prop="catId">
          <el-input v-model="form.catId" placeholder="请输入商品分类ID" />
        </el-form-item>
        <el-form-item label="商品分类名称" prop="catName">
          <el-input v-model="form.catName" placeholder="请输入商品分类名称" />
        </el-form-item>
        <el-form-item label="商品详情" prop="goodsDesc">
          <el-input v-model="form.goodsDesc" placeholder="请输入商品详情" />
        </el-form-item>
        <el-form-item label="商品编号" prop="goodsSn">
          <el-input v-model="form.goodsSn" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="商店成本价格" prop="costPrice">
          <el-input v-model="form.costPrice" placeholder="请输入商店成本价格" />
        </el-form-item>
        <el-form-item label="商品系列ID" prop="seriesId">
          <el-input v-model="form.seriesId" placeholder="请输入商品系列ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->

    <el-drawer title="预览" :visible.sync="preview.open" direction="rtl" size="375px">
      <div class="preview-box">
        <div class="g-pic">
          <el-carousel height="375px">
            <el-carousel-item v-for="item in preview.swiperImage" :key="item.id">
              <img :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="g-info">
          <div class="g-price"><span>¥</span>{{preview.detail.price}}</div>
          <div class="g-name">{{preview.detail.goodsName}}</div>
        </div>
        <div class="g-desc">
          <div class="g-desc-img" v-for="item in preview.detailImage" :key="item.id">
            <img v-if="item.fileType!='mp4'" :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            <video v-if="item.fileType=='mp4'" :src="item.fileFullUrl"></video>
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods, exportGoods, setSeriesId, tbGoodsPrice, tbAllGoods, tbCgtpGoods } from "@/api/goods/goods";
import { getFileList} from "@/api/basic/file"
export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 商品基础信息表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      isActiveOptions: [],
      // 是否上架字典
      isOnSaleOptions: [],
      supplierOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        isOnSale: undefined,
        catId: undefined,
        catName: undefined,
        goodsSn: undefined,
        seriesId: undefined,
        goodsSupplierName:undefined
      },
      /* // 表单参数
      form: {},
      // 表单校验
      rules: {
      }, */
      preview:{
        open:false,
        swiperImage:[],
        detailImage:[],
        detail:{}
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
    this.getDicts("is_active").then(response => {
      this.isOnSaleOptions = response.data;
    });
    this.getDicts("YD_GYS_LIST").then(response => {
      this.supplierOptions = response.data;
    });
  },
  methods: {
    /** 查询商品基础信息列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    // 是否上架字典翻译
    isOnSaleFormat(row, column) {
      return this.selectDictLabel(this.isOnSaleOptions, row.isOnSale);
    },
    // 取消按钮
    /* cancel() {
      this.open = false;
      this.reset();
    }, */
    // 表单重置
    /* reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isActive: undefined,
        lesseeId: undefined,
        goodsName: undefined,
        isOnSale: undefined,
        price: undefined,
        goodsImg: undefined,
        sort: undefined,
        catId: undefined,
        catName: undefined,
        goodsDesc: undefined,
        goodsSn: undefined,
        costPrice: undefined,
        seriesId: undefined
      };
      this.resetForm("form");
    }, */
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      /* this.reset();
      this.open = true;
      this.title = "添加商品基础信息"; */
      this.$router.push('/LsY/goods/edit/new')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      /* this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品基础信息";
      }); */
      this.$router.push('/LsY/goods/edit/'+(row.id||this.ids))
    },
    /** 提交按钮 */
    /* submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateGoods(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGoods(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    }, */
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商品基础信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGoods(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品基础信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGoods(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    setSeries(){
        setSeriesId({ids:this.ids.join()}).then(response => {
            if (response.code !== 200) {
                this.msgError(response.msg)
            }else{
                this.msgSuccess("操作成功")
            }
        })
    },
    setPriceToShop(row){
      this.loading = true
      const ids = row.id || this.ids;
      tbGoodsPrice({ids}).then(response => {
        this.loading = false
          if (response.code !== 200) {
              this.msgError(response.msg)
          }else{
              this.msgSuccess("操作成功")
          }
      })
    },
    tbAllGoods(){
      this.loading = true
      setTimeout(() => {
        this.msgSuccess("后台正在同步所有商品，时间较长，请稍后再刷新数据。")
      }, 2000);
      tbAllGoods().then(response => {
        this.loading = false
        if (response.code !== 200) {
            this.msgError(response.msg)
        }else{
            this.msgSuccess("操作成功")
        }
        this.handleQuery()
      })
    },
    tbCgtpGoods(){
      this.loading = true
      setTimeout(() => {
        this.msgSuccess("后台正在同步采购平台商品，时间较长，请稍后再刷新数据。")
      }, 2000);
      tbCgtpGoods().then(response => {
        this.loading = false
        if (response.code !== 200) {
            this.msgError(response.msg)
        }else{
            this.msgSuccess("操作成功")
        }
        this.handleQuery()
      })
    },
    openPreview(row){
      this.preview.open = true
      this.preview.swiperImage = []
      this.preview.detailImage = []
      this.preview.detail = row
      getFileList({
        sourcessId: row.id,
        sourcessType: 'GOODS_INFO',
        sourcessType2: 'SWIPER'
      }).then(res => {
        this.preview.swiperImage= res.rows
      })
      getFileList({
        sourcessId: row.id,
        sourcessType: 'GOODS_INFO',
        sourcessType2: 'DESCRIBE'
      }).then(res => {
        this.preview.detailImage= res.rows
      })
    }
  }
};
</script>

<style lang="scss" scoped>
  .el-icon-remove{
    font-size: 36px;
    color: #F56C6C;
  }
  .el-icon-circle-plus{
    font-size: 36px;
    color: #409EFF
  }
  .g-pic img{
    width: 100%;
    height: auto;
    vertical-align: top;
  }
  .g-desc img{
    width: 100%;
    height: auto;
    vertical-align: top;
  }
  .g-info{
    padding: 18px 16px;
    background-color: #FFFFFF;
    margin-bottom: 6px;
  }
  .g-price{
    font-size: 22px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #FF5263;
  }
  .g-price span{
    font-size: 12px;
  }
  .g-name{
    font-size: 18px;
    font-family: PingFang SC;
    font-weight: 500;
    color: #000000;
  }
  .el-drawer__body{
    background-color: #F3F5F7;
    height: 100%;
    overflow: auto;
  }
</style>
